package demo1;
import java.util.*
/**
 * @Author liangzai
 * @Description:
 */
public class Test {
    public boolean IsPopOrder (int[] pushA, int[] popA) {
        // write code here
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for (int i = 0; i < pushA.length; i++) {
            stack.push(pushA[i]);
            while (!stack.empty() && j < popA.length
                    && stack.peek() == popA[j]) {
                stack.pop();
                j++;
            }
        }
        //return j >= popA.length;
        return stack.empty();
    }

    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        //1. 遍历字符串
        for(int i = 0;i < s.length();i++) {
            char ch = s.charAt(i);
            //2.是不是左括号
            if(ch == '(' || ch == '[' || ch == '{') {
                stack.push(ch);
            }else {
                //3.右括号
                //3.1 栈为空
                if(stack.isEmpty()) {
                    return false;
                }
                //3.2 栈不为空
                char ch2 = stack.peek();//左括号
                if(ch == ')' && ch2 == '('  || ch == ']'
                        && ch2 == '[' || ch == '}' && ch2 == '{') {
                    stack.pop();
                }else {
                    return false;
                }
            }
        }
        //字符串遍历完了 栈还是不为空
        if(!stack.isEmpty()) {
            return false;
        }
        return true;
    }


    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for(int i = 0;i < tokens.length;i++) {
            String str = tokens[i];
            if(!isOperations(str)) {
                //不是运算符  说明是数字
                int val = Integer.valueOf(str);
                stack.push(val);
            }else {
                //是运算符
                int num2 = stack.pop();
                int num1 = stack.pop();
                switch(str) {
                    case "+":
                        stack.push(num1+num2);
                        break;
                    case "-":
                        stack.push(num1-num2);
                        break;
                    case "*":
                        stack.push(num1*num2);
                        break;
                    case "/":
                        stack.push(num1/num2);
                        break;
                }
            }
        }

        return stack.pop();
}
